<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
            <div class="layui-form-item">




            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;" class="layuiadmin-btn-useradmin">
                <button class="layuiadmin-btn-useradmin layui-btn" data-type="add" id="add">添加</button>
            </div>
            <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>

            <script type="text/html" id="table-useradmin-admin">

                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-4"></i>删除</a>
            </script>


        </div>
    </div>
</div>

<script>
    layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table', 'upload'], function() {

        var $ = layui.$,
            admin = layui.admin,
            view = layui.view,
            setter = layui.setter,
            table = layui.table,
            upload = layui.upload,
            form = layui.form;


        form.render(null, 'layadmin-userfront-formlist');
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            //执行重载
            table.reload('table-instance', {
                where: field
            });
        });

        //事件

        var active = {
            add: function() {
                admin.popup({
                    title: '添加提示消息',
                    area: ['550px', '600px'],
                    id: 'LAY-popup-user-add',
                    success: function(layero, index) {
                        view(this.id).render('prompt/form').done(function() {
                            form.render(null, 'layuiadmin-form-scenicinfo');
                            //监听提交
                            form.on('submit(LAY-user-back-submit)', function(data) {
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                editdmHandle(Object.assign(data, field));
                                layui.table.reload('LAY-user-manage'); //重载表格
                                layer.close(index); //执行关闭 
                            });
                        });
                    }
                });
            }
        };
        table.render({
            elem: "#table-instance",
            url: setter.request.baseUrl + "prompt/list",
            method: "get",
            headers: {
                token: layui.data("layuiAdmin").token
            },
            response: {
                statusName: "code",
                statusCode: 0,
                msgName: "msg",
                countName: "totalCount",
                dataName: "prompt",
            },
            cols: [
                [{
                    field: "title",
                    title: "标题"
                }, {
                    field: "content",
                    title: "内容"
                }, {
                    field: "createdate",
                    title: "创建时间"
                }, {
                    fixed: "right",
                    title: "操作",
                    toolbar: "#table-useradmin-admin"
                }, ],
            ],
            id: 'table-instance',
            page: true,
        });
        //删除
        table.on("tool(table-instance)", function(obj) {
            var data = obj.data;
            if (obj.event === "del") {
                layer.confirm("真的删除么", function(index) {
                    deldmHandle({
                        id: data.id,
                    });
                    obj.del();
                    layui.table.reload(); //重载表格
                    layer.close(index);
                });
            } else if (obj.event === "edit") {
                admin.popup({
                    title: "编辑DM",
                    area: ["550px", "600px"],
                    success: function(layero, index) {
                        view(this.id)
                            .render("prompt/form", data)
                            .done(function() {
                                let _data = data;
                                form.render(null, "layuiadmin-form-user");

                                form.on('submit(LAY-user-back-submit)', function(data) {

                                    var field = data.field;
                                    // warning
                                    editdmHandle(Object.assign(_data, field));
                                    // layui.table.reload('LAY-user-back-manage'); //重载表格
                                    layer.close(index);
                                });
                            });
                    },
                });
            }
        });


        //编辑
        function editdmHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "prompt/save",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }

        //删除
        function deldmHandle(params) {
            // debugger
            admin.req({
                url: setter.request.baseUrl + "prompt/delete",
                type: "get",
                data: {
                    ...params,
                },
                done: function(res) {
                    layer.msg(res.msg);
                },
            });
        }


        $('.layuiadmin-btn-useradmin .layui-btn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>